package com.zzh.lib.views.listener;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.customview.widget.ViewDragHelper;

/**
 * Created by ZZH on 2022/5/9.
 *
 * @Date: 2022/5/9
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description:
 */
public abstract class HDragViewCallback extends ViewDragHelper.Callback {
    private final View dragView, slideRailContainer;

    public HDragViewCallback(View dragView, View slideRailContainer) {
        this.dragView = dragView;
        this.slideRailContainer = slideRailContainer;
    }

    @Override
    public boolean tryCaptureView(View child, int pointerId) {
        //判断能拽托的View，这里会遍历内部子控件来决定是否可以拽托，我们只需要滑块可以滑动
        return child == dragView;
    }

    @Override
    public int clampViewPositionHorizontal(View child, int left, int dx) {
        //拽托子View横向滑动时回调，回调的left，则是可以滑动的左上角x坐标
        int lockBtnWidth = dragView.getWidth();
        //限制左右临界点
        int fullWidth = slideRailContainer.getWidth();
        //最少的左边
        int leftMinDistance = slideRailContainer.getPaddingStart();
        //最多的右边
        int leftMaxDistance = fullWidth - slideRailContainer.getPaddingEnd() - lockBtnWidth;
        //修复两端的临界值
        if (left < leftMinDistance) {
            return leftMinDistance;
        } else if (left > leftMaxDistance) {
            return leftMaxDistance;
        }
        return left;
    }

    @Override
    public int clampViewPositionVertical(View child, int top, int dy) {
        //拽托子View纵向滑动时回调，锁定顶部padding距离即可，不能不复写，否则少了顶部的padding，位置就偏去上面了
        int lockBtnHeight = dragView.getHeight();
        int fullHeight = slideRailContainer.getHeight();
        int topMinDistance = slideRailContainer.getPaddingTop();
        int topMaxDistance = fullHeight - slideRailContainer.getPaddingBottom() - lockBtnHeight;
        if (top < topMinDistance) {
            return topMinDistance;
        } else if (top > topMaxDistance) {
            return topMaxDistance;
        }
        return top;
    }

    @Override
    public int getViewHorizontalDragRange(@NonNull View child) {
        return 1;
    }

    @Override
    public int getViewVerticalDragRange(@NonNull View child) {
        return 1;
    }
}
